package com.ych.modules.api.authorInfc.user.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ych.SDK.alibaba.alipaySDK.entity.AlipayQueryOrderInfo_Request;
import com.ych.modules.base.dbEntity.TUserAccountRechargeOrderEntity;
import com.ych.modules.common.entity.query.QueryAccountRechargeOrderEntity;
import com.ych.modules.common.service.UserAccountRechargeOrderService;
import com.ych.utils.TLMap;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/2
 * Time: 12:15
 * Description:
 */
@RestController("apiUserAccountRechargeOrderController")
@RequestMapping("/api/auth/user/recharge/order")
@Api(tags = {"API——用户充值订单相关"}, description = "API——用户充值订单相关")
public class UserAccountRechargeOrderController {
    @Autowired
    private UserAccountRechargeOrderService userAccountRechargeOrderService;

    @PostMapping("/listRechargeOrder")
    @ApiOperation(value = "获取充值记录列表", notes = "获取充值记录列表")
    public R<Page<TUserAccountRechargeOrderEntity>> listRechargeOrder() {
        try {
            QueryAccountRechargeOrderEntity entity = new QueryAccountRechargeOrderEntity();
            entity.setUserId(TLMap.getUserId());
            Page<TUserAccountRechargeOrderEntity> page = userAccountRechargeOrderService.listAccountRechargeOrder(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @ApiOperation(value = "创建订单并调用支付", position = 1)
    @PostMapping("/createOrder")
    public R<String> createOrder(@RequestBody TUserAccountRechargeOrderEntity entity) {
        try {
            entity.setUserId(TLMap.getUserId());
            String body = userAccountRechargeOrderService.createOrder(entity);
            return new R(0, "成功", body);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        } catch (Exception e) {
            return new R(2, "系统未知异常" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "处理订单支付回调", position = 2)
    @PostMapping("/dealOrderCallback")
    public R<String> dealOrderCallback(@RequestBody AlipayQueryOrderInfo_Request orderEntity) {
        try {
            if (null == orderEntity) {
                throw new RuntimeException("对象为空！");
            }
            userAccountRechargeOrderService.dealOrderCallback(orderEntity);
            return new R(0, "成功", null);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        } catch (Exception e) {
            return new R(2, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "充值订单退款", position = 1)
    @PostMapping("/refundOrder")
    public R<String> refundOrder(@RequestBody TUserAccountRechargeOrderEntity refundEntity) {
        try {
            userAccountRechargeOrderService.refundOrder(refundEntity);
            return new R(0, "订单处理中", null);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        } catch (Exception e) {
            return new R(2, "系统未知异常" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "测试Mq")
    @PostMapping("/testMq")
    public R<String> testMq(){
        try {
            userAccountRechargeOrderService.testMq();
            return new R(0,"成功",null);
        }catch (Exception e){
            return new R(1,e.getMessage(),null);
        }
    }
}
